package com.bac.rentmap.ad.flat.image;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.bac.rentmap.ad.flat.FlatAd;
import com.bac.rentmap.ad.flat.crawler.FlatCrawledAd;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class FlatImage {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;

    @Persistent
    private Blob image;

    @Persistent
    private Blob thumbnail;

    @Persistent
    private FlatImageType type;

    /**
     * Required for persistent object referencing image.
     */
    @Persistent
    private Date publishingDate;

    /**
     * Reference to {@link FlatAd} or to {@link FlatCrawledAd}.
     */
    @Persistent
    private Key flat;

    @Persistent
    private String sessionId;
    
    @SuppressWarnings("unused")
    @Persistent
    private Integer orderNo;

    public FlatImage(FlatImageType type, String sessionId, Blob image,
            Blob thumbnail) {
        this.type = type;
        orderNo = type.ordinal();
        this.sessionId = sessionId;
        this.image = image;
        this.thumbnail = thumbnail;
    }

    public Blob getImage() {
        return image;
    }

    public FlatImageType getType() {
        return type;
    }

    public Key getFlat() {
        return flat;
    }

    public void setFlat(Key flat) {
        sessionId = null;
        this.flat = flat;
    }

    public void setImage(Blob image) {
        this.image = image;
    }

    /**
     * for unit tests only
     */
    public Long getId() {
        return id;
    }

    /**
     * for unit tests only
     */
    String getSessionId() {
        return sessionId;
    }

    public Blob getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(Blob thumbnail) {
        this.thumbnail = thumbnail;
    }

    public Date getPublishingDate() {
        return publishingDate;
    }

    public void setPublishingDate(Date publishingDate) {
        if(publishingDate == null){
          this.publishingDate = null;
        }
        else{
            this.publishingDate = new Date(publishingDate.getTime());
        }
    }
}
